package com.yxq.mdt.app.modules.market.model;

import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.market.bean.MallResponseBean;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.model.MainPersonalApi;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.Query;

public class MarketModel implements IBaseModel {
    //获取个人信息
    public void getPersonalDate(String token, ApiModel.CommonCallback<MyPersonalBean> callback) {
        RetrofitFactory.getJavaRetrofit().create(MainPersonalApi.class)
                .getPersonalModel(token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MyPersonalBean>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(MyPersonalBean respBase) {
                        callback.onSuccess(respBase);
                    }
                });
    }

    public void getMallMarketList(ApiModel.CommonCallback<MallResponseBean> callback) {
        RetrofitFactory.getRetrofit().create(MarketApi.class)
                .getMallMarketList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<MallResponseBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<MallResponseBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 商品-商品分页查询
     *
     * @param page      页码
     * @param sort      排序（0综合 1最新 2价格 3销量）
     * @param sort_type 排序类型（-1默认，0 升序，1降序）
     * @param callback
     */
    public void getMallMarketGoodsList(int page, int sort, int sort_type, ApiModel.CommonCallback<MallGoodsBean> callback) {
        RetrofitFactory.getRetrofit().create(MarketApi.class)
                .getMallMarketGoodsList(page, sort, sort_type)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<MallGoodsBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<MallGoodsBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 商品-商品分页查询
     *
     * @param page      页码
     * @param sort      排序（0综合 1最新 2价格 3销量）
     * @param sort_type 排序类型（-1默认，0 升序，1降序）
     * @param callback
     */
    public void getMallMarketGoodsList(String cat_id, int page, int sort, int sort_type, ApiModel.CommonCallback<MallGoodsBean> callback) {
        RetrofitFactory.getRetrofit().create(MarketApi.class)
                .getMallMarketGoodsList(cat_id, page, sort, sort_type)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<MallGoodsBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<MallGoodsBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {

    }
}
